package com.project.common.core.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;

/**
 * Created by gyx on 18/7/18.
 */
public class JsonUtils {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    private static final ObjectMapper simpleBigDecimalObjectMapper = new ObjectMapper();


    static {
        SimpleModule module = new SimpleModule();
        module.addSerializer(new StdSerializer<BigDecimal>(BigDecimal.class) {
            @Override
            public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider provider) throws IOException {
                gen.writeNumber(value.stripTrailingZeros().toPlainString());
            }
        });
        simpleBigDecimalObjectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        simpleBigDecimalObjectMapper.registerModule(module);

        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }


    /**
     * java对象转json
     */
    public static String toJson(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            //log.error("JSON序列化异常 obj:" + obj, e);
            return null;
        }
    }


    /**
     * java对象转json, 简化金额序列化
     */
    public static String toJsonSimpleBigDecimal(Object obj) {
        try {
            return simpleBigDecimalObjectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            //log.error("JSON序列化异常 obj:" + obj, e);
            return null;
        }
    }


    /**
     * json转java对象
     */
    public static <T> T toJavaObject(String text, Class<T> clazz) {
        try {
            return objectMapper.readValue(text, clazz);
        } catch (IOException e) {
            //log.error("JSON反序列化异常 text:" + text, e);
            return null;
        }
    }

    /**
     * json转java对象
     */
    public static <T> T toJavaObject(String text, TypeReference valueTypeRef) {
        try {
            return (T) objectMapper.readValue(text, valueTypeRef);
        } catch (IOException e) {
            //log.error("JSON反序列化异常 text:" + text, e);
            return null;
        }
    }


    /**
     * json转 JsonNode 对象
     */
    public static JsonNode readTree(String text) {
        try {
            return objectMapper.readTree(text);
        } catch (IOException e) {
            //log.error("JSON readTree 异常 text:" + text, e);
            return null;
        }
    }





}
